package com.suyang.syt.actools.template.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.actools.template.model.dto.TemplateDTO;
import com.suyang.syt.actools.template.service.interfaces.TemplateService;
import com.suyang.syt.model.User;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.StringUtil;

/**
 * @author tme_author
 * @date tme_date
 * @description tme_description
 */
@Controller
@RequestMapping("/admin/template")
public class TemplateController {
	@Autowired
	private TemplateService templateService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "template/list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<TemplateDTO> list(HttpServletRequest request, int page, int rows, String sort, String order) {
		try {
			String createDate1 = request.getParameter("createDate1");
			String createDate2 = request.getParameter("createDate2");
			return templateService.listByEasyUI(page, rows, sort, order, createDate1, createDate2);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj add(HttpServletRequest request, TemplateDTO templateDTO) {
		User user = (User) request.getSession().getAttribute("loginUser");
		try {
			templateDTO.setCreateName(user.getUsername());
			templateDTO.setCreateTime(DateFormat.dateToString(new Date()));
			return templateService.add(templateDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj delete(HttpServletRequest request, String ids) {
		try {
			return templateService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj update(HttpServletRequest request, TemplateDTO templateDTO) {
		User user = (User) request.getSession().getAttribute("loginUser");
		try {
			templateDTO.setModifyName(user.getUsername());
			templateDTO.setModifyTime(DateFormat.dateToString(new Date()));
			return templateService.update(templateDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}
}
